<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Equity extends ORM
{
    public static function equity_list($equity_id='')
    {
        //系统默认的
        $ty_model = DB::select('id','title','icon')->from('member_equity')
            ->where('is_system', '=', '1');
        if (!St_Functions::is_normal_app_install('memberdiscount')) {
            $ty_model->and_where('id', '!=', 2);
        }
        if($equity_id){
            //传这个时表明只查等级相关的权益
            $ty_model->and_where(DB::expr(" and id IN({$equity_id}) "));
        }
        $ty_list  = $ty_model->order_by(DB::expr(" IFNULL(sort,99999) ASC"))->execute()->as_array();

        //用户设置的
        $set_model = DB::select('id','title','icon')->from('member_equity')
            ->where('is_system', '=', 0);
        if($equity_id){
            //传这个时表明只查等级相关的权益
            $set_model->and_where(DB::expr(" and id IN({$equity_id}) "));
        }
        $set_model->order_by(DB::expr(" IFNULL(sort,99999) ASC"));
        $set_list = $set_model->execute()->as_array();

        $zdy_list = DB::select()->from('member_equity_icon')->execute()->as_array();

        $data = [
            'ty_list'  => $ty_list,
            'set_list' => $set_list,
            'zdy_list' => $zdy_list,
        ];

        return $data;
    }

    public static function get_equity_icon_info($id)
    {
        return DB::select()->from('member_equity_icon')->where('id', '=', $id)->execute()->current();
    }

    public static function get_equity_info($id)
    {
        return DB::select()->from('member_equity')->where('id', '=', $id)->execute()->current();
    }
}